package com.lcu.wl.modules.sys.service.impl;

import com.lcu.wl.common.utils.MapUtils;
import com.lcu.wl.modules.sys.entity.UserRole;
import com.lcu.wl.modules.sys.mapper.UserRoleMapper;
import com.lcu.wl.modules.sys.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户与角色对应关系 服务实现类
 * </p>
 *
 * @author wanglei
 * @since 2021-01-15
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        //先删除用户与角色关系
        this.removeByMap(new MapUtils().put("user_id", userId));

        if(roleIdList == null || roleIdList.size() == 0){
            return;
        }

        //保存用户与角色关系
        for(Long roleId : roleIdList){
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);

            this.save(userRole);
        }
    }

    @Override
    public List<Long> getRoleIdList(Long userId) {
        return baseMapper.selectRoleIdList(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long userId) {
        this.removeByMap(new MapUtils().put("user_id",userId));
    }

    @Override
    public int delByRoleIdsBatch(Long[] roleIds) {
        return baseMapper.delByRoleIdsBatch(roleIds);
    }

}
